﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication.UserControls
{
    public partial class PrompTextBox : TextBox
    {
        public PrompTextBox()
        {
            InitializeComponent();
        }

        public string PrompText { get; set; }

        private bool IsPrompText = false;

        protected override void OnMouseEnter(EventArgs e)
        {
            if (string.IsNullOrEmpty(Text) || Text == PrompText)
            {
                ClearPrompText();
            }
            base.OnMouseEnter(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            if (string.IsNullOrEmpty(Text) && Text != PrompText)
            {
                SetPrompText();
            }
            base.OnMouseLeave(e);
        }

        protected override void OnGotFocus(EventArgs e)
        {
            if (IsPrompText)
            {
                ClearPrompText();
                IsPrompText = true;
            }
            base.OnGotFocus(e);
        }

        protected override void OnLostFocus(EventArgs e)
        {
            if (string.IsNullOrEmpty(Text) && Text != PrompText)
            {
                SetPrompText();
            }
            base.OnLostFocus(e);
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (string.IsNullOrEmpty(Text) && Text != PrompText)
            {
                SetPrompText();
            }
        }

        private void SetPrompText()
        {
            Text = PrompText;
            ForeColor = Color.Gray;
        }

        private void ClearPrompText()
        {
            Text = string.Empty;
            ForeColor = Color.Black;
        }
    }
}
